package com.lantu.core.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantu.annotation.CurrentUser;
import com.lantu.core.entity.User;
import com.lantu.dto.member.MemberDTO;
import com.lantu.core.entity.Member;
import com.lantu.core.service.IMemberService;
import com.lantu.dto.member.MemberPageDTO;
import com.lantu.utils.BeanUtils;
import com.lantu.vo.member.MemberListVO;
import com.lantu.vo.member.MemberVO;
import com.lantu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys/member")
@CrossOrigin   //跨域处理
public class MemberController {
    @Autowired
    private IMemberService memberService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping("/list")
    public Result<Map<String,Object>> getMemberList(MemberPageDTO dto){
        Page<Member> page = memberService.getMemberPage(dto);

        List<MemberListVO> list = BeanUtils.toBean(page.getRecords(), MemberListVO.class);

        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows", list);
        return Result.success(data);
    }


    @PostMapping
    public Result<?> saveMember(@RequestBody MemberDTO dto, @CurrentUser User loginUser) {
        if(ObjectUtil.isNotEmpty(dto)){
            if(StrUtil.isEmpty(dto.getId())){
                if (memberService.addMember(loginUser, dto)) {
                    return Result.success("新增会员成功");
                }
                return Result.fail("新增失败");
            }else{
                if(memberService.updateMember(loginUser, dto)){
                    return Result.success("修改会员成功");
                }
                return Result.fail("修改失败");
            }
        }
        return Result.fail("操作失败");
    }


    @GetMapping("/{id}")
    public Result<MemberVO> getMemberById(@PathVariable("id") Integer id){
        return Result.success(MemberVO.newInstance(memberService.getById(id)));
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteMemberById(@PathVariable("id") Integer id){
        memberService.removeById(id);
        return Result.success("删除会员成功");
    }

    @PostMapping("/updateStatus")
    public Result<?> updateMemberStatus(@RequestBody MemberDTO dto, @CurrentUser User loginUser){
        if(ObjectUtil.isNotEmpty(dto) && StrUtil.isNotEmpty(dto.getId())){
            Member sourceMember = memberService.getById(dto.getId());
            if(ObjectUtil.isNotEmpty(sourceMember) && StrUtil.isNotEmpty(sourceMember.getId())){
                sourceMember.setStatus(dto.getStatus());
                sourceMember.updateBy(loginUser.getUsername());
                memberService.updateById(sourceMember);
                return Result.success("操作成功");
            }
        }
        return Result.fail("操作失败");
    }


    @PostMapping("/resetPassword")
    public Result<?> resetMemberPassword(@RequestBody MemberDTO dto, @CurrentUser User loginUser){
        if(ObjectUtil.isNotEmpty(dto) && StrUtil.isNotEmpty(dto.getId())){
            Member sourceMember = memberService.getById(dto.getId());
            if(ObjectUtil.isNotEmpty(sourceMember) && StrUtil.isNotEmpty(sourceMember.getId())){
                if(StrUtil.isNotEmpty(dto.getPassword())){
                    sourceMember.setPassword(passwordEncoder.encode(dto.getPassword()));
                    sourceMember.updateBy(loginUser.getUsername());
                    memberService.updateById(sourceMember);
                    return Result.success("重置成功");
                }
            }
        }
        return Result.fail("操作失败");
    }
}
